package com.gwclh.personnel.controller;


import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gwclh.personnel.bean.ResultVo;
import com.gwclh.personnel.domain.PageDomain;
import com.gwclh.personnel.entity.Dismission;
import com.gwclh.personnel.entity.Employee;
import com.gwclh.personnel.entity.Internship;
import com.gwclh.personnel.service.IDismissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.gwclh.personnel.bean.ResultVo;
import com.gwclh.personnel.entity.Dismission;
import com.gwclh.personnel.entity.Employee;
import com.gwclh.personnel.service.IDismissionService;
import com.gwclh.personnel.service.IEmployeeService;
import com.gwclh.personnel.shiro.AccountProfile;
import com.gwclh.personnel.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 员工离职记录表 前端控制器
 * </p>
 *
 * @author gzhhh
 * @since 2020-07-24
 */
@RestController
@RequestMapping("/dismissions")
public class DismissionController extends BaseController {
    @Autowired
    IDismissionService dismissionService;
    @Autowired
    IEmployeeService employeeService;

    @PostMapping("/selectEmp")
    @ApiOperation("查询员工(员工id，员工名称，部门id，部门名称)")
    public ResultVo selectEmp() {
        IPage<Map<String, Object>> mapIPage = dismissionService.selectEmp();
        Assert.notNull(mapIPage, "未查询到任何员工信息");
        return ResultVo.success(mapIPage);
    }

    @PostMapping("/selectEmpBySearch")
    @ApiOperation("根据条件查询员工")
    public ResultVo selectEmpBySearch(String deptName, Integer empId, String empName) {
        IPage<Map<String, Object>> mapIPage = dismissionService.selectEmpBySearch(deptName, empId, empName);
        Assert.notNull(mapIPage, "根据此条件未查询到员工信息");
        return ResultVo.success(mapIPage);
    }

    @PostMapping("/quit")
    @ApiOperation("对员工进行离职操作")
    public ResultVo quit(@RequestBody Dismission dismission) {
        Integer quit = dismissionService.quit(dismission);
        Assert.notNull(quit, "离职操作失败");
        return ResultVo.success("离职操作成功");
    }

    @PostMapping("/selectQuit")
    @ApiOperation("查询离职员工")
    public ResultVo selectQuit() {
        IPage<Map<String, Object>> mapIPage = dismissionService.selectQuit();
        Assert.notNull(mapIPage, "未查询到有离职员工");
        return ResultVo.success(mapIPage);
    }

    @PostMapping("/selectQuitBySearch")
    @ApiOperation("根据条件查询离职员工")
    public ResultVo selectQuitBySearch(Integer empId, String deptName, String staName) {
        IPage<Map<String, Object>> mapIPage = dismissionService.selectQuitBySearch(empId, deptName, staName);
        Assert.notNull(mapIPage, "未按照此条件查询到离职员工");
        return ResultVo.success(mapIPage);
    }

    @GetMapping("/data/{pId}")
    public ResultVo getDismDataByPId(@PathVariable(value = "pId") Integer pId) {
        List<Map<String, Object>> dismissionDataByPid = dismissionService.getDismissionDataByPid(pId);
        return ResultVo.success(dismissionDataByPid);
    }

    @PostMapping("")
    public ResultVo addDismission(@RequestBody @Validated Dismission dismission) {
        AccountProfile profile = ShiroUtil.getProfile();
        dismission.setOperId(profile.getId());
        boolean save = dismissionService.save(dismission);
        Assert.isTrue(save, "离职失败！");

        Employee byId = employeeService.getById(dismission.getEmpId());
        if (dismission.getIsToTalent()) {
            byId.setIsTalents(dismission.getIsToTalent());
        }
        byId.setStatus(0);
        boolean update = employeeService.updateById(byId);
        Assert.isTrue(update, "离职失败！");

        return ResultVo.success();
    }

    @GetMapping("")
    public ResultVo pageDismission(Dismission dismission, HttpServletRequest request) {
        PageDomain pageDomain = getPageParams(request);
        Map<String, Object> map = dismissionService.pageDismissions(pageDomain, dismission);
        return ResultVo.success(map);
    }

    @DeleteMapping("/{id}")
    public ResultVo deleteDismission(@PathVariable(value = "id") Integer id) {
        Dismission byId = dismissionService.getById(id);
        boolean b1 = employeeService.removeById(byId.getEmpId());
        boolean b = dismissionService.removeById(id);
        Assert.isTrue(b && b1, "删除失败");
        return ResultVo.success();
    }
}
